<?php
namespace app\index\controller;

use think\Controller;
use think\Session;
use app\common\lib\upload\upload;

class Index  extends  Controller
{
    public function index()
    {

        //Session::delete('user_token');
        $url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        $this->token_get($url);

        return $this->fetch();
    }
    public function search()
    {
        //$url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        //$this->token_get($url);
    $token='111';
                             $this->assign('token',$token);
        return $this->fetch();
    }
    public function search_result()
    {
        //$url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        //$this->token_get($url);
         $token='111';
                                     $this->assign('token',$token);

        return $this->fetch();
    }
    public function go_pay()
    {
        //$appId='wx49cfc16a07e40442';
        //$this->assign('signPackage',$signPackage);
        $url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        $this->token_get($url);
        return $this->fetch();
    }

    public function  token_get($url)
    {
        $token=Session::get('user_token');
        //获得当前Url;
        if(!$token)
        {
            Session::set('user_url',$url);
            $this->getServerToken($url);
        }
        $this->assign('token',$token);
    }
    public function goods_detail()
    {
       $url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
       $this->token_get($url);
        return $this->fetch();
    }
    public function category()
    {
        $url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        $this->token_get($url);
        return $this->fetch();
    }
    public function list_recommend()
    {
        $url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        $this->token_get($url);
        return $this->fetch();
    }
    public function add()
        {
            $url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
            $this->token_get($url);

            return $this->fetch();
        }
        public function serviceAgreement()
                {
                    $url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
                    $this->token_get($url);

                    return $this->fetch();
                }
        public function logisticsinfo_ex()
                {
                   $url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
                   $this->token_get($url);
                    return $this->fetch();
                }
        public function logistics_information()
                {
                    $url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
                   $this->token_get($url);
                    return $this->fetch();
                }
        public function aboutus()
                        {
                            $url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
                            $this->token_get($url);

                            return $this->fetch();
                        }
        public function information_details()
                        {
                            $url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
                            $this->token_get($url);

                            return $this->fetch();
                        }
        public function information_video_details()
                        {
                            $url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
                            $this->token_get($url);

                            return $this->fetch();
                        }
        public function review()
        {
           $url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
           $this->token_get($url);
           return $this->fetch();
        }
    public function cart()
    {
        $url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
       $this->token_get($url);

        return $this->fetch();
    }
     public function  memberindex()
     {
         $url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
         $this->token_get($url);

         return $this->fetch();
     }
     public function checkout()
     {
         $url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
         $this->token_get($url);

         return $this->fetch();
     }
    public function order_list()
    {
        $url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        $this->token_get($url);

        return $this->fetch();
    }
    public function  order_details()
    {
        $url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        $this->token_get($url);

        return $this->fetch();
    }

     public function discount()
        {
            $url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
            $this->token_get($url);
            return $this->fetch();
        }
    public function add_list()
    {
        $url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        $this->token_get($url);
        return $this->fetch();
    }

    public function information()
    {
        $token='111';
        $this->assign('token',$token);
        return $this->fetch();
    }


        public function goods_evaluate()
        {
            $url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
            $this->token_get($url);
            return $this->fetch();
        }
    public function pay_result()
    {
        $url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        $this->token_get($url);
        return $this->fetch();
    }
    public function getServerToken()
    {
        $code = input('param.code');
		if(empty($code))
		{
            $urllist="http://admin.chinlingcloud.com/index/index/getServerToken";
            $redirecturl= urlencode($urllist);
            $appId="wx49cfc16a07e40442";
            $wx_url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appId.'&redirect_uri='.$redirecturl.'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
            header("Location:$wx_url");
            exit;
		}else{
            $appId = "wx49cfc16a07e40442";
            $secret = "902657db3b77b41da662c396ef85d7b8";
            $res=$this->getOpenid($code,$appId,$secret);
        $ConfigResUrl = "https://program.chinlingcloud.com/index.php/api/v2/getToken";
        $getServerToken = curl_post_https($ConfigResUrl,$res);
         $getServerToken=  trim($getServerToken,'"');
        $user_token=Session::set('user_token',$getServerToken);
           $_is=Session::has('user_token');
            if($_is)
            {
                $token_url=Session::get('user_url');
                if(!$token_url)
                {
                    $token_url='http://admin.chinlingcloud.com/index/index/index';
                }
                header("Location:$token_url");
                exit;
            }
        }
    }
    //验证token
    public function getVerifyToken()
    {
        $ConfigResUrl = "https://program.chinlingcloud.com/index.php/api/v2/VerifyToken";
        $token = $_POST['token'];
        $data = array(
            'token' => $token
        );
        $verifyToken = curl_post_https($ConfigResUrl, $data);
        $data=array(
            'states'=>$verifyToken
        );
        echo json_encode($data);
    }
    public function getWxCode()
    {
        //$urllist='http://'.$_SERVER['HTTP_HOST'].''.$_SERVER['REQUEST_URI'];

    }
    function getOpenid($code,$appId,$secret)
    {
        //return $code;

        //$header("Location:https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$secret."&code='.$code.'&grant_type=authorization_code");
        $get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appId.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL,$get_token_url);
        curl_setopt($ch,CURLOPT_HEADER,0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        $res = curl_exec($ch);
        curl_close($ch);
        $json_obj = json_decode($res,true);
        return $json_obj;
    }
    public function uploadifyImg(){
        //halt($_FILES);
        $image = Upload::image($_FILES,$_POST['urlRoute'],$_POST['qy'],$_POST['is_whether']);
        //图片样式处理
        //$fops = 'imageView2/1/w/800/h/100';
        $fops="";
        // 捕获异常
        try{
            // 返回qiniu上的文件名

        }catch(\Exception $e){
            return json(['state'=>0,'message'=>$e->getMessage()]);
        }
        // 返回给uploadify插件状态
        if($image){
            $data = [
                'qy'=>$image['qy'],
                'state' => 1,
                'is_whether'=>$image['img_value'],
                'message' => 'OK',
                'path' => config('qiniu.image_url').'/'.$image['img'].'?'.$fops,
            ];
            return json($data);exit;
        }else{
            return json(['state'=>0,'message'=>'上传失败']);
        }
    }
}
